﻿@model List<SW.Lite.Model.Master.RoleModel>
@using SW.Lite.Core.Extensions
@{
    Layout = "~/Areas/Master/Views/Shared/_Layout.cshtml";
}

@section Header{
    <script type="text/javascript">
        function addMenu() {
            $("#addDialog").dialog({
                title: '添加角色',
                modal: true,
                width: 600,
                buttons: [
                    {
                        text: "确定",
                        click: function () {
                            Utils.ajax({
                                url: '@Url.Action("Add")',
                                data: { 'Code': $('#Code').val(), 'Title': $('#Title').val() },
                                type: 'POST',
                                success: function (d) { if (d.Result) location.reload(); },
                                error: function (status) { }
                            });
                        }
                    }, {
                        text: '取消',
                        click: function () {
                            $(this).dialog("close");
                        }
                    }
                ]
            });
        }

        function deleteAll() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length < 0) {
                Utils.alert('消息', '请选择要删除的角色！');
                return;
            }

            Utils.confirm('请确认', '确定删除选定的角色吗？', function () {
                Utils.ajax({
                    url: '@Url.Action("Delete")',
                    action: 'ADD',
                    data: { 'ids': menuIDs.join(',') },
                    type: 'POST',
                    success: function (d) {
                        if (d.Result) {
                            Utils.alert('消息', '已删除', function () {
                                location.reload();
                            });
                        }
                    },
                    error: function (status) { }
                });
            });
        }

        function editMenu() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length != 1) {
                Utils.alert('消息', '请选择要修改的角色，且只能选择一项！');
                return;
            }

            Utils.ajax({
                url: '@Url.Action("Single")',
                data: { 'id': menuIDs[0] },
                type: 'POST',
                success: function (menu) {
                    Utils.setObject2Dom(menu);
                    $('#Code').attr('readonly', 'readonly');
                    $('#Code').addClass('disabled');
                    $("#addDialog").dialog({
                        title: '编辑角色',
                        modal: true,
                        width: 600,
                        buttons: [
                            {
                                text: "确定",
                                click: function () {
                                    Utils.ajax({
                                        url: '@Url.Action("Edit")',
                                        data: { 'ID': $('#ID').val(), 'Code': $('#Code').val(), 'Title': $('#Title').val() },
                                        type: 'POST',
                                        success: function (d) { if (d.Result) location.reload(); },
                                        error: function (status) { }
                                    });
                                }
                            }, {
                                text: '取消',
                                click: function () {
                                    $(this).dialog("close");
                                }
                            }
                        ]
                    });
                },
                error: function (status) { }
            });
        }

        function menuRoles() {
            var menuIDs = [];
            $('.cboItem:checked').each(function (i, n) {
                menuIDs.push($(n).val());
            });
            if (menuIDs.length != 1) {
                Utils.alert('消息', '请选择要查看菜单权限的角色，且只能选择一项！');
                return;
            }

            location.href = '@Url.Action("RoleMenu")?ID=' + menuIDs[0];
        }
    </script>
}
<div class="panel grid_8">
    <div class="panel-header">
        <span class="i-24 i-user">角色管理</span>
    </div>
    <div class="panel-body">
        <div class="panel-toolbar top clearfix">
            <ul>
                <li><a id="btnAdd" class="ic-16 ic-add" onclick="addMenu();">添加</a></li>
                <li><a id="btnAdd" class="ic-16 ic-edit" onclick="editMenu();">编辑</a></li>
                <li><a id="btnDelete" class="ic-16 ic-chart-organisation" onclick="menuRoles();">菜单权限</a></li>
                <li><a id="btnDelete" class="ic-16 ic-cross" onclick="deleteAll();">删除</a></li>
            </ul>
        </div>
        <div class="dataTables_wrapper">
            <table class="datatable-fn table">
                <thead>
                    <tr>
                        <th style="width: 30px;"><input type="checkbox" onchange="Utils.checkAll(this.checked);" /></th>
                        <th style="width: 200px;">角色编码</th>
                        <th style="width: auto;">角色名称</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td class="center"><input class="cboItem" type="checkbox" value="@item.Id" /></td>
                            <td class="center">@item.Code</td>
                            <td class="center">@item.Title</td>
                        </tr>
                    }
                </tbody>
            </table>
            <div class="dataTables_paginate paging_full_numbers">
                <span>
                    @Html.Pager(20, (int)ViewBag.RecordCount)
                </span>
            </div>
        </div>
    </div>
</div>
<div id="addDialog" class="hide">
    <div class="form-inline form">
        <div class="form-row">
            <label>角色编码</label>
            <div class="form-item large">
                <input id="Code" class="textinput" type="text" /><input type="hidden" id="ID" />
            </div>
        </div>
        <div class="form-row">
            <label>角色名称</label>
            <div class="form-item large">
                <input id="Title" class="textinput" type="text" />
            </div>
        </div>
    </div>
</div>